#include "tiling/platform/platform_ascendc.h"
#include "scatter_elements_tiling.h"
#include "register/op_def_registry.h"

#include <vector>
#include <cstring>
#include <cstdlib>
#include <algorithm>
namespace optiling {
const uint32_t BLOCK_SIZE = 32;
static ge::graphStatus TilingFunc(gert::TilingContext* context)
{
    ScatterElementsTilingData tiling;
    int32_t NUM = 16;
    uint32_t sizeofdatatype;
    uint32_t totalLengthAligned;
    auto ascendcPlatform = platform_ascendc::PlatformAscendC(context->GetPlatformInfo());
    auto socVersion = ascendcPlatform.GetSocVersion();
    uint64_t ub_size;
    ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, ub_size);
    auto aivNum = ascendcPlatform.GetCoreNum();
    std::cout<<"aivNum:"<<aivNum<<std::endl;
    int axis = *context->GetAttrs()->GetInt(0);
    const char *reduce = context->GetAttrs()->GetAttrPointer<char>(1);
    auto var_dimshape = context->GetInputTensor(0)->GetOriginShape();
    auto indices_shape = context->GetInputTensor(1)->GetOriginShape();
    auto dt = context->GetInputTensor(0)->GetDataType();
    if(dt == ge::DT_UINT8){
        sizeofdatatype = 1;
        // NUM =12;
    }else if(dt == ge::DT_FLOAT16 || dt == ge::DT_BF16){
        sizeofdatatype = 2;
        NUM=6;
    }else{
        sizeofdatatype = 4;
        NUM=6;
    }
    uint32_t strcase = 0;
    if(strcmp(reduce, "add") == 0){
        strcase = 1;
    }
    else{
        strcase = 2;
    }
    uint32_t dim_shape = indices_shape.GetDimNum();

    std::cout<<"dim_shape:"<<dim_shape<<std::endl;
    std::cout<<"axis:"<<axis<<std::endl;
    uint32_t var_shape = var_dimshape.GetDimNum();
    uint32_t var_bats;
    uint32_t var_tier;
    uint32_t var_rows;
    uint32_t var_cols;
    uint32_t loop_h;
    uint32_t loop_i;
    uint32_t loop_j;
    uint32_t loop_k;
    if(dim_shape == 1){
        // context->SetTilingKey(1);     //一维的就是loopCount,只有i，dim=0的情况
    }
    else if(dim_shape == 2){
        // context->SetTilingKey(2);     //二维循环loop_i，loop_j，有i，j，dim=0,dim=1的情况
        loop_i = indices_shape.GetDim(0);   
        loop_j = indices_shape.GetDim(1);
        var_rows = var_dimshape.GetDim(0);
        var_cols = var_dimshape.GetDim(1);
    }
    else if(dim_shape == 3){
        // context->SetTilingKey(3);     //三维循环loop_i，loop_j，loop_k， 有i，j，k，dim=0，dim=1，dim=2的情况
        loop_i = indices_shape.GetDim(0);
        loop_j = indices_shape.GetDim(1);
        loop_k = indices_shape.GetDim(2);
        var_tier = var_dimshape.GetDim(0);
        var_rows = var_dimshape.GetDim(1);
        var_cols = var_dimshape.GetDim(2);
    }
    else if(dim_shape == 4){
        // context->SetTilingKey(3);     //三维循环loop_i，loop_j，loop_k， 有i，j，k，dim=0，dim=1，dim=2的情况
        loop_h = indices_shape.GetDim(0);
        loop_i = indices_shape.GetDim(1);
        loop_j = indices_shape.GetDim(2);
        loop_k = indices_shape.GetDim(3);
        var_bats = var_dimshape.GetDim(0);
        var_tier = var_dimshape.GetDim(1);
        var_rows = var_dimshape.GetDim(2);
        var_cols = var_dimshape.GetDim(3);
    }
    uint32_t lastdim = context->GetInputTensor(2)->GetShapeSize() / context->GetInputTensor(1)->GetShapeSize();
    std::cout<<"lastdim:"<<lastdim<<std::endl;

    tiling.set_lastdim(lastdim);

    uint32_t totalLength = context->GetInputTensor(1)->GetShapeSize();
    std::cout<<"indices_size:"<<totalLength<<std::endl;

    uint32_t ALIGN_NUM = BLOCK_SIZE / sizeofdatatype;
    uint32_t tiling_size = ((ub_size) / BLOCK_SIZE / 2) / NUM;
    tiling_size = tiling_size <= 8 ? tiling_size : tiling_size / 8 * 8;

    uint32_t block_size = tiling_size * ALIGN_NUM;
    aivNum = (aivNum < totalLength / block_size) ? aivNum : (totalLength / block_size);
    std::cout<<"aivNum:"<<aivNum<<std::endl;
    aivNum = aivNum >= 1 ? aivNum : 1;
    std::cout<<"aivNum:"<<aivNum<<std::endl;
    uint32_t core_size = (totalLength / aivNum) / (ALIGN_NUM * 8) * (ALIGN_NUM * 8);
    uint32_t core_remain = totalLength - aivNum * core_size;

    totalLength = context->GetInputTensor(0)->GetShapeSize();
    tiling.set_totalLength(totalLength);
    tiling.set_ALIGN_NUM(ALIGN_NUM);
    tiling.set_tiling_size(tiling_size);
    tiling.set_block_size(block_size);
    tiling.set_aivNum(aivNum);
    tiling.set_core_size(core_size);
    tiling.set_core_remain(core_remain);
    tiling.set_strcase(strcase);
    tiling.set_axis(axis);
    
    tiling.set_var_shape(var_shape);
    tiling.set_var_bats(var_bats);
    tiling.set_var_tier(var_tier);
    tiling.set_var_rows(var_rows);
    tiling.set_var_cols(var_cols);
    
    tiling.set_dim_shape(dim_shape);
    tiling.set_loop_h(loop_h);
    tiling.set_loop_i(loop_i);
    tiling.set_loop_j(loop_j);
    tiling.set_loop_k(loop_k);
    context->SetBlockDim(aivNum);
    
    tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
    context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());
    size_t *currentWorkspace = context->GetWorkspaceSizes(1);
    currentWorkspace[0] = 0;

    return ge::GRAPH_SUCCESS;
}
}


namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context)
{
    const gert::Shape* x1_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
}

namespace ops {
class ScatterElements : public OpDef {
public:
    explicit ScatterElements(const char* name) : OpDef(name)
    {
        this->Input("var")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_INT32, ge::DT_UINT8})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("indices")
            .ParamType(REQUIRED)
            .DataType({ge::DT_INT32, ge::DT_INT32, ge::DT_INT32, ge::DT_INT32})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("updates")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_INT32, ge::DT_UINT8})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        // this->Output("var")
        //     .ParamType(REQUIRED)
        //     .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_INT32, ge::DT_UINT8})
        //     .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
        //     .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Attr("axis").AttrType(OPTIONAL).Int(0);
        this->Attr("reduce").AttrType(OPTIONAL).String("None");

        this->SetInferShape(ge::InferShape);

        this->AICore()
            .SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend310b");

    }
};

OP_ADD(ScatterElements);
}
